package cn.iocoder.yudao.module.pplan.controller.admin.recipe;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pplan.controller.admin.recipe.vo.*;
import cn.iocoder.yudao.module.pplan.dal.dataobject.recipe.RecipeDO;
import cn.iocoder.yudao.module.pplan.service.recipe.RecipeService;

@Tag(name = "管理后台 - 生产计划-餐谱数据管理")
@RestController
@RequestMapping("/pplan/recipe")
@Validated
public class RecipeController {

    @Resource
    private RecipeService recipeService;

    @PostMapping("/create")
    @Operation(summary = "创建生产计划-餐谱数据管理")
    @PreAuthorize("@ss.hasPermission('pplan:recipe:create')")
    public CommonResult<Long> createRecipe(@Valid @RequestBody RecipeSaveReqVO createReqVO) {
        return success(recipeService.createRecipe(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产计划-餐谱数据管理")
    @PreAuthorize("@ss.hasPermission('pplan:recipe:update')")
    public CommonResult<Boolean> updateRecipe(@Valid @RequestBody RecipeSaveReqVO updateReqVO) {
        recipeService.updateRecipe(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产计划-餐谱数据管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pplan:recipe:delete')")
    public CommonResult<Boolean> deleteRecipe(@RequestParam("id") Long id) {
        recipeService.deleteRecipe(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生产计划-餐谱数据管理")
                @PreAuthorize("@ss.hasPermission('pplan:recipe:delete')")
    public CommonResult<Boolean> deleteRecipeList(@RequestParam("ids") List<Long> ids) {
        recipeService.deleteRecipeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产计划-餐谱数据管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pplan:recipe:query')")
    public CommonResult<RecipeRespVO> getRecipe(@RequestParam("id") Long id) {
        RecipeRespVO recipeRespVO = recipeService.getRecipeRespVO(id);
        return success(recipeRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产计划-餐谱数据管理分页")
    @PreAuthorize("@ss.hasPermission('pplan:recipe:query')")
    public CommonResult<PageResult<RecipeRespVO>> getRecipePage(@Valid RecipePageReqVO pageReqVO) {
        PageResult<RecipeDO> pageResult = recipeService.getRecipePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecipeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产计划-餐谱数据管理 Excel")
    @PreAuthorize("@ss.hasPermission('pplan:recipe:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecipeExcel(@Valid RecipePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecipeDO> list = recipeService.getRecipePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产计划-餐谱数据管理.xls", "数据", RecipeRespVO.class,
                        BeanUtils.toBean(list, RecipeRespVO.class));
    }

}